package org.example;

import org.example.business.LoginBusiness;
import org.example.ui.LoadingPanel;
import org.example.ui.customer.BottomPanel;
import org.example.ui.customer.PersonalPanel;

import javax.swing.*;
import java.awt.event.*;
import java.io.File;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        File file = new File("data");
        if (!file.exists()) {
            new ReadAll().readAll();
        }
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setBounds(100, 30, 1200,800);
        LoginBusiness.panel.setLayout(LoginBusiness.layout);
        LoginBusiness.jFrame = jFrame;
        jFrame.setVisible(false);
        LoginBusiness.dialog.setVisible(true);
        LoginBusiness.dialog.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                LoginBusiness.panel.add(new LoadingPanel(), "0");
                LoginBusiness.jFrame.setVisible(true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        LoginBusiness.bottomPanel = new BottomPanel();
                        LoginBusiness.panel.add(LoginBusiness.bottomPanel, "1");
                        LoginBusiness.panel.add(new PersonalPanel(), "2");
                        LoginBusiness.layout.show(LoginBusiness.panel, "1");
                        LoginBusiness.panel.setVisible(false);
                        LoginBusiness.panel.setVisible(true);
                        LoginBusiness.lock.b = false;
                    }
                }).start();
                LoginBusiness.layout.show(LoginBusiness.panel, "0");
                LoginBusiness.panel.setVisible(false);
                LoginBusiness.panel.setVisible(true);
            }
        });
        LoginBusiness.dialog.setVisible(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    LoginBusiness.lock.onLock();
                    LoginBusiness.jFrame.setVisible(false);
                    LoginBusiness.panel.removeAll();
                    LoginBusiness.panel.setVisible(false);
                    LoginBusiness.panel.setVisible(true);
                    LoginBusiness.dialog.setVisible(true);
                }
            }
        }).start();
        jFrame.add(LoginBusiness.panel);
    }
}